package com.peans.common.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;

public enum RandomUtils {
	;
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUMBERCHAR = "0123456789";

	/**
	 * 返回一个定长的随机字符串(只包含大小写字母、数字)
	 * @param length  随机字符串长度
	 * @return  随机字符串
	 */
	public static String generateString(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 生成a至b中间的数字
	 * @param a
	 * @param b
	 * @return
	 */
	public static BigDecimal generateBetween(BigDecimal a, BigDecimal b) {
		// M+Math.random()*(N-M)
		BigDecimal result = a.add(new BigDecimal(Math.random()).multiply(b.subtract(a)));
		return result;
	}

	/**
	 * 返回一个定长的随机纯字母字符串(只包含大小写字母)
	 *
	 * @param length 随机字符串长度
	 * @return 随机字符串
	 */
	public static String generateMixString(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
	 *
	 * @param length 随机字符串长度
	 * @return 随机字符串
	 */
	public static String generateLowerString(int length) {
		return generateMixString(length).toLowerCase();
	}

	/**
	 * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
	 *
	 * @param length 随机字符串长度
	 * @return 随机字符串
	 */
	public static String generateUpperString(int length) {
		return generateMixString(length).toUpperCase();
	}

	/**
	 * 生成一个定长的纯0字符串
	 *
	 * @param length
	 *            字符串长度
	 * @return 纯0字符串
	 */
	public static String generateZeroString(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append('0');
		}
		return sb.toString();
	}

	/**
	 * 根据数字生成一个定长的字符串，长度不够前面补0
	 *
	 * @param num 数字
	 * @param fixdlenth 字符串长度
	 * @return 定长的字符串
	 */
	public static String toFixdLengthString(long num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
		} else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 根据数字生成一个定长的字符串，长度不够前面补0
	 *
	 * @param num 数字
	 * @param fixdlenth 字符串长度
	 * @return 定长的字符串
	 */
	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
		} else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 每次生成的len位数都不相同
	 *
	 * @param param
	 * @return 定长的数字
	 */
	public static int getNotSimple(int[] param, int len) {
		Random rand = new Random();
		for (int i = param.length; i > 1; i--) {
			int index = rand.nextInt(i);
			int tmp = param[index];
			param[index] = param[i - 1];
			param[i - 1] = tmp;
		}
		int result = 0;
		for (int i = 0; i < len; i++) {
			result = result * 10 + param[i];
		}
		return result;
	}

	/**
	 * 随机生存一个纯数字多位随机数
	 *
	 * @param length
	 * @return
	 */
	public static String getIntegerSimple(int length) {
		Random random = new Random(System.currentTimeMillis());
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int n = random.nextInt(10);
			buffer.append(n);
		}
		return buffer.toString();
	}

	/**
	 * 从集合中随机选一
	 *
	 * @param <T>
	 * @param results
	 * @return
	 */
	public static <T> T getInCollections(T[] results) {
		Random random = new Random();
		int index = random.nextInt(results.length);
		return results[index];
	}

	public static <T> T getInCollections2(List<T> results) {
		Random random = new Random();
		int index = random.nextInt(results.size());
		return results.get(index);
	}

	public static String getChinese() {
		String str = null;
		int highPos, lowPos;
		Random random = new Random();
		highPos = (176 + Math.abs(random.nextInt(39)));
		lowPos = 161 + Math.abs(random.nextInt(93));
		byte[] b = new byte[2];
		b[0] = (new Integer(highPos)).byteValue();
		b[1] = (new Integer(lowPos)).byteValue();
		try {
			str = new String(b, "GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String getFixedLengthChinese(int length) {
		String str = "";
		for (int i = length; i > 0; i--) {
			str = str + getChinese();
		}
		return str;
	}

	public static String getRandomLengthChiness(int start, int end) {
		String str = "";
		int length = new Random().nextInt(end + 1);
		if (length < start) {
			str = getRandomLengthChiness(start, end);
		} else {
			for (int i = 0; i < length; i++) {
				str = str + getChinese();
			}
		}
		return str;
	}

	public static Date randomDate(String beginDate, String endDate) {
		try {
			Date start = DateUtils.parseOfDay(beginDate);
			Date end = DateUtils.parseOfDay(endDate);
			if (start.getTime() >= end.getTime()) {
				return null;
			}
			long date = random(start.getTime(), end.getTime());
			return new Date(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static long random(long begin, long end) {
		long rtnn = begin + (long) (Math.random() * (end - begin));
		if (rtnn == begin || rtnn == end) {
			return random(begin, end);
		}
		return rtnn;
	}

	public static void main(String[] args) {
		int a = RandomUtils.getRanNum(1000, 50000);
		System.out.println(new BigDecimal(a));
	}

	/**
	 * 返回[begin,end]之间的一个随机整数
	 * 
	 * @return
	 */
	public static int getRanNum(int min, int max) {
		Random rdm = new Random();
		return rdm.nextInt(max - min + 1) + min;
	}

	/**
	 * 获得一个13位随机手机号码
	 *
	 * @return
	 */
	public static String getRanPhoneNum() {
		Integer[] phoneStartNum = new Integer[] { 13, 14, 15, 18, 170 };
		int phoneNumPart1 = getInCollections(phoneStartNum);
		String phoneNum = null;
		if (phoneNumPart1 == 170) {
			phoneNum = phoneNumPart1 + getIntegerSimple(8);
		} else {
			phoneNum = phoneNumPart1 + getIntegerSimple(9);
		}
		return phoneNum;
	}
	
	/**
	 * 随机生成一个浮点型
	 * @param min
	 * @param max
	 * @param x
	 * @return
	 */
	public static double getRandomDouble(double min,double max,double x){
		double r = 0;
		do {
			r = x*Math.random() + min;
		} while (r > max);
		return r;
	}

}
